from flask import Blueprint, current_app, g, render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, length

# 创建用户蓝图对象
user_bp = Blueprint('user', __name__, url_prefix='/user')


class LoginForm(FlaskForm):
    username = StringField('用户名', render_kw={'placeholder': '请输入用户名'}, validators=[DataRequired()])
    password = PasswordField('密码', render_kw={'placeholder': '请输入密码'},
                             validators=[DataRequired(), length(1, 6)])
    remember = BooleanField('记住我')
    submit = SubmitField('登录')


@user_bp.route('/list')
def list():
    # 访问应用的配置
    debug_mode = current_app.config['DEBUG']
    print(f'DEBUG模式：{debug_mode}')
    print(g.get('username'))
    return '用户列表'


@user_bp.route('/index')
def index():
    return render_template('index.html')


@user_bp.route('/detail')
def detail():
    return render_template('detail.html')


@user_bp.route('/login', methods=['GET', 'POST'])
def login():
    print(request.method)
    loginForm = LoginForm()
    print("标签：", loginForm.username.label)
    print("表单", loginForm.username)
    if request.method == 'GET':
        return render_template('login.html', form=loginForm)
    else:
        if loginForm.validate():
            username = loginForm.username.data
            password = loginForm.password.data
            print(username, password)
        return 'ok'
